package com.bee.user.service.impl;

import com.bee.auth.client.AuthHolder;
import com.bee.user.mapper.ResourceMapper;
import com.bee.user.common.domain.ResourceDO;
import com.bee.user.common.vo.ResourceVO;
import com.bee.user.service.ResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2019-08-27 10:19:14
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<ResourceDO> queryByRoleId(Long roleId) {
        return resourceMapper.queryByRoleId(roleId);
    }

    @Override
    public ResourceDO queryById(Long id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public PageInfo<ResourceDO> queryPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ResourceDO> list = resourceMapper.queryAll();
        return new PageInfo<>(list);
    }

    @Override
    public ResourceDO insert(ResourceVO domain) {

        ResourceDO resourceDO = new ResourceDO();
        BeanUtils.copyProperties(domain, resourceDO);

        resourceDO.setCreateTime(new Date());
        resourceDO.setCreator(AuthHolder.getUser());
        resourceDO.setUpdateTime(resourceDO.getCreateTime());
        resourceDO.setUpdator(resourceDO.getCreator());
        resourceMapper.insert(resourceDO);
        return resourceDO;
    }

    @Override
    public void update(ResourceVO domain) {

        ResourceDO resourceDO = new ResourceDO();
        BeanUtils.copyProperties(domain, resourceDO);

        resourceDO.setUpdator(AuthHolder.getUser());
        resourceDO.setUpdateTime(new Date());
        resourceMapper.updateById(resourceDO);
    }

    @Override
    public boolean deleteById(Long id) {
        ResourceDO resourceDO = queryById(id);
        resourceDO.setDeleted(Boolean.TRUE);
        resourceDO.setDeleteTime(new Date());
        return resourceMapper.updateById(resourceDO) > 0;
    }
}